package MAgentAssignment;

import MAgentAssignment.gui.AirportIcon;
import MAgentAssignment.ontology.FlightOntology;
import MAgentAssignment.ontology.actions.CreateAirline;
import MAgentAssignment.ontology.actions.CreateAirpot;
import MAgentAssignment.ontology.concepts.Airline;
import MAgentAssignment.ontology.concepts.Airport;
import MAgentAssignment.ontology.concepts.Location;
import jade.content.lang.Codec;
import jade.content.lang.sl.SLCodec;
import jade.content.onto.Ontology;
import jade.core.AID;
import jade.core.Agent;
import jade.core.behaviours.OneShotBehaviour;
import jade.lang.acl.ACLMessage;
import jade.wrapper.AgentContainer;
import jade.wrapper.AgentController;
import jade.wrapper.StaleProxyException;

import java.io.IOException;
import java.util.Vector;

/**
 * Created by IntelliJ IDEA.
 * User: Prageeth
 * Date: 2/16/12
 * Time: 9:52 PM
 * To change this template use File | Settings | File Templates.
 */
public class CreatorAgent extends Agent
{
    private Codec codec = new SLCodec();
    private Ontology ontology = FlightOntology.getInstance();
    private AgentController agentController;
    private Vector<Airport> airports = new Vector<Airport>();
    private Vector<Airline> airlines = new Vector<Airline>();

    @Override protected void setup()
    {

        getContentManager().registerLanguage( codec );
        getContentManager().registerOntology( ontology );
        populateVectors();


        addBehaviour( new OneShotBehaviour()
        {
            @Override public void action()
            {
                createAirlines();

                createAirports();
            }
        } );
    }

    public void createAirports()
    {
        AgentContainer container = (AgentContainer) getContainerController(); // get a container controller for creating new agents
        for( Airport airport : airports )
        {
            try
            {
                String airportManagerName = "Airport : " + airport.getAirpotCode();
                agentController = container.createNewAgent( airportManagerName, "MAgentAssignment.AirportManagerAgent", new Object[]{airport} );
                agentController.start();

                ACLMessage msg = new ACLMessage( ACLMessage.INFORM );
                CreateAirpot createAirpot = new CreateAirpot();
                createAirpot.setAirport( airport );
                try
                {
                    msg.setContentObject( createAirpot );
                }
                catch( IOException e )
                {
                    e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                }
                msg.addReceiver( new AID( airportManagerName, AID.ISLOCALNAME ) );
                send( msg );

            }
            catch( StaleProxyException e )
            {
                e.printStackTrace();
            }
        }
    }

    public void createAirlines()
    {
        AgentContainer container = (AgentContainer) getContainerController(); // get a container controller for creating new agents
        try
        {
            agentController = container.createNewAgent( "GUI", "MAgentAssignment.gui.InterfaceAgent", null );
            agentController.start();
        }
        catch( StaleProxyException e )
        {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }

        for( Airline airline : airlines )
        {
            try
            {
                String airlineManagerName = "Airline : " + airline.getAirLineCode();
                agentController = container.createNewAgent( airlineManagerName, "MAgentAssignment.AirlineManagerAgent", new Object[]{airline} );
                agentController.start();

//                ACLMessage msg = new ACLMessage( ACLMessage.INFORM );
//                CreateAirline createAirline = new CreateAirline();
//                createAirline.setAirline( airline );
//                try
//                {
//                    msg.setContentObject( createAirline );
//                }
//                catch( IOException e )
//                {
//                    e.printStackTrace();
//                }
//                msg.addReceiver( new AID( airlineManagerName, AID.ISLOCALNAME ) );
//                send( msg );

            }
            catch( StaleProxyException e )
            {
                e.printStackTrace();
            }
        }
    }

    public Vector<Airport> getAirports()
    {
        return airports;
    }

    public void setAirports( Vector<Airport> airports )
    {
        this.airports = airports;
    }

    public Vector<Airline> getAirlines()
    {
        return airlines;
    }

    public void setAirlines( Vector<Airline> airlines )
    {
        this.airlines = airlines;
    }

    private void populateVectors()
    {
        Airport newDelhi = new Airport();
        newDelhi.setAirportName( "New Delhi" );
        newDelhi.setAirpotCode( "DEL" );
        newDelhi.setParkingSlots( 20 );
        newDelhi.setLocation( new Location( 10, 10 ) );
        airports.add( newDelhi );
//
        Airport checnnai = new Airport();
        checnnai.setAirportName( "Chennai" );
        checnnai.setAirpotCode( "CEN" );
        checnnai.setParkingSlots( 20 );
        checnnai.setLocation( new Location( 10, 200 ) );
        airports.add( checnnai );

        Airport colombo = new Airport();
        colombo.setAirportName( "Colombo" );
        colombo.setAirpotCode( "CMB" );
        colombo.setParkingSlots( 20 );
        colombo.setLocation( new Location( 400, 10 ) );
        airports.add( colombo );

        Airport hambanthota = new Airport();
        hambanthota.setAirportName( "Hambanthota" );
        hambanthota.setAirpotCode( "HMB" );
        hambanthota.setParkingSlots( 20 );
        hambanthota.setLocation( new Location( 123, 300 ) );
        airports.add( hambanthota );

        Airport male = new Airport();
        male.setAirportName( "Male" );
        male.setAirpotCode( "MAL" );
        male.setParkingSlots( 20 );
        male.setLocation( new Location( 100, 350 ) );
        airports.add( male );

        Airport gatwick = new Airport();
        gatwick.setAirportName( "Gatwick" );
        gatwick.setAirpotCode( "GWT" );
        gatwick.setParkingSlots( 20 );
        gatwick.setLocation( new Location( 300, 300 ) );
        airports.add( gatwick );

        Airline airline = new Airline();
        airline.setAirLineCode( "UL" );
        airline.setAirLineName( "Sri Lankan Airline" );
        airline.setMaxNumberOfFlight( 4 );
        airlines.add( airline );

        airline = new Airline();
        airline.setAirLineCode( "IN" );
        airline.setAirLineName( "Indian Airline" );
        airline.setMaxNumberOfFlight( 5 );
        airlines.add( airline );

        airline = new Airline();
        airline.setAirLineCode( "BX" );
        airline.setAirLineName( "British Airways" );
        airline.setMaxNumberOfFlight( 5 );
        airlines.add( airline );

        airline = new Airline();
        airline.setAirLineCode( "MH" );
        airline.setAirLineName( "Mihin Air" );
        airline.setMaxNumberOfFlight( 2 );
        airlines.add( airline );

    }
}
